﻿using System;
using eBusiness.Models;

namespace eBusiness.App_DAL
{
    public class UnitOfWork : IDisposable
    {
        private readonly ebusinessEntities _context = new ebusinessEntities();
        private GenericRepository<Product> _productRepository;
        private GenericRepository<Category> _categoryRepository;
        private GenericRepository<Status> _statusRepository;
        private GenericRepository<Manufacture> _manufactureRepository;
        private GenericRepository<News> _newRepository;
        private GenericRepository<FeedUser> _feedUserRepository;
        private GenericRepository<SaleManagement> _saleManagementRepository;

        public GenericRepository<News> NewsRepository
        {
            get { return _newRepository ?? (_newRepository = new GenericRepository<News>(_context)); }
        }

        public GenericRepository<Status> StatusRepository
        {
            get { return _statusRepository ?? (_statusRepository = new GenericRepository<Status>(_context)); }
        }
        public GenericRepository<Manufacture> ManufactureRepository
        {
            get { return _manufactureRepository ?? (_manufactureRepository = new GenericRepository<Manufacture>(_context)); }
        }

        public GenericRepository<FeedUser> FeedUserRepository
        {
            get { return _feedUserRepository ?? (_feedUserRepository = new GenericRepository<FeedUser>(_context)); }
        }

        public GenericRepository<SaleManagement> SaleRepository
        {
            get { return _saleManagementRepository ?? (_saleManagementRepository = new GenericRepository<SaleManagement>(_context)); }
        }

        public GenericRepository<Category> CategoryRepository
        {
            get { return _categoryRepository ?? (_categoryRepository = new GenericRepository<Category>(_context)); }
        }


        public GenericRepository<Product> ProductRepository
        {
            get { return _productRepository ?? (_productRepository = new GenericRepository<Product>(_context)); }
        }

        public void Save()
        {
            _context.SaveChanges();
        }

        private bool _disposed;

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _context.Dispose();
                }
            }
            _disposed = true;
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}